package com.masterclient.app.modules.order;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;

/**
 * 作者：hxm
 * 创建时间：2017/9/5 10:18
 * 功能说明：
 */
public class FreightSelectDialog extends Dialog implements View.OnClickListener {

    private ImageView ivPayNow;
    private ImageView ivPayArrive;
    private TextView tvPayNow;
    private TextView tvPayArrive;

    private SelectFreightListener mSelectFreightListener;
    private int freightType;

    public FreightSelectDialog(@NonNull Context context,int freightType,SelectFreightListener selectFreightListener) {
        super(context, R.style.MyDialog);
        this.freightType = freightType;
        mSelectFreightListener = selectFreightListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_freight_select);
        setCanceledOnTouchOutside(true);

        View viewPayNow = findViewById(R.id.view_pay_now);
        View viewPayArrive = findViewById(R.id.view_pay_arrive);
        View ivCancel = findViewById(R.id.iv_cancel);
        tvPayNow = findViewById(R.id.tv_pay_now);
        tvPayArrive = findViewById(R.id.tv_pay_arrive);
        ivPayNow = findViewById(R.id.iv_pay_now);
        ivPayArrive = findViewById(R.id.iv_pay_arrive);

        if (freightType == 1){
            ivPayNow.setVisibility(View.VISIBLE);
            ivPayArrive.setVisibility(View.GONE);
            tvPayNow.setTextColor(Color.parseColor("#FF6809"));
            tvPayArrive.setTextColor(Color.parseColor("#141922"));
        }else if (freightType == 2){
            ivPayNow.setVisibility(View.GONE);
            ivPayArrive.setVisibility(View.VISIBLE);
            tvPayNow.setTextColor(Color.parseColor("#141922"));
            tvPayArrive.setTextColor(Color.parseColor("#FF6809"));
        }else {
            ivPayNow.setVisibility(View.GONE);
            ivPayArrive.setVisibility(View.GONE);
            tvPayNow.setTextColor(Color.parseColor("#141922"));
            tvPayArrive.setTextColor(Color.parseColor("#141922"));
        }
        viewPayNow.setOnClickListener(this);
        viewPayArrive.setOnClickListener(this);
        ivCancel.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        window.setWindowAnimations(R.style.bottomShow);
        WindowManager.LayoutParams windowParams = window.getAttributes();
        window.setGravity(Gravity.BOTTOM);
        windowParams.width = ScreenUtils.getScreenWidth();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowParams);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.view_pay_now:
                ivPayNow.setVisibility(View.VISIBLE);
                ivPayArrive.setVisibility(View.GONE);
                tvPayNow.setTextColor(Color.parseColor("#FF6809"));
                tvPayArrive.setTextColor(Color.parseColor("#141922"));
                mSelectFreightListener.onSelectFreight(1,tvPayNow.getText().toString());
                dismiss();
                break;
            case R.id.view_pay_arrive:
                ivPayNow.setVisibility(View.GONE);
                ivPayArrive.setVisibility(View.VISIBLE);
                tvPayNow.setTextColor(Color.parseColor("#141922"));
                tvPayArrive.setTextColor(Color.parseColor("#FF6809"));
                mSelectFreightListener.onSelectFreight(2,tvPayArrive.getText().toString());
                dismiss();
            case R.id.iv_cancel:
                dismiss();
                break;
        }
    }

    public interface SelectFreightListener {
        void onSelectFreight(int freightType,String txt);
    }
}
